{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script>\n",
       "code_show=true; \n",
       "function code_toggle() {\n",
       " if (code_show){\n",
       " $('div.input').hide();\n",
       " } else {\n",
       " $('div.input').show();\n",
       " }\n",
       " code_show = !code_show\n",
       "} \n",
       "$( document ).ready(code_toggle);\n",
       "</script>\n",
       "<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code for QISKit exercises.\"></form>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import HTML\n",
    "\n",
    "HTML('''<script>\n",
    "code_show=true; \n",
    "function code_toggle() {\n",
    " if (code_show){\n",
    " $('div.input').hide();\n",
    " } else {\n",
    " $('div.input').show();\n",
    " }\n",
    " code_show = !code_show\n",
    "} \n",
    "$( document ).ready(code_toggle);\n",
    "</script>\n",
    "<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code for QISKit exercises.\"></form>''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7. Quantum teleportation\n",
    "\n",
    "\n",
    "Quantum teleportation was first described in [1], it allows the transfer of an unknown quantum state of a qubit between two parties. In order to do so, the two parties, Alice and Bob, must first share an entangled pair of qubits in a Bell state. Alice can then transfer the unknown quantum state of a qubit to Bob by measuring her share of the entangled pair and the qubit she wants to transfer. Depending on the outcome of these measurements, she then tells Bob, through classical communication, which gates he should apply to his qubit in order to convert the state of his share of the entangled pair of qubit into the unknown state Alice wished to transfer.\n",
    "Another algorithm, called superdense coding, which allows to encode 2 bits of classical information in one qubit has been described in [2]. This again involves two parties initially sharing an entangled pair of qubits in the Bell state. Alice can then apply any of the four Pauli gates (I,X,Y,Z) to her qubit and then send it to Bob. When Bob jointly measures the two qubits, he can learn which operator was used by Alice. Therefore, he can read 2 bits of information.\n",
    "These algorithms are somewhat similar to each other, the key new feature of both is that they exploit the non-classical correlation arising in entangled states as a resource for communication."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.1 Quantum teleportation algorithm\n",
    "\n",
    "<img src=\"figures/6/teleportation2.jpeg\"  width=\"600\">\n",
    "$$\\text{1. Quantum circuit for quantum teleportation.}$$\n",
    "\n",
    "Let's take a look at the details of the quantum teleportation algorithm.\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "Let us Assume that Alice (A) and Bob (B) each have a qubit from a shared entangled pair whose wave function is\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert\\Phi^{+} \\rangle =\\frac{1}{\\sqrt{2}}( \\lvert 0\\rangle_A \\lvert 0 \\rangle_B+ \\lvert 1\\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "\n",
    "Alice wants to teleport another qubit in an unknown quantum state $ \\lvert \\phi \\rangle$ to Bob.\n",
    "\n",
    "\\begin{equation}\n",
    " \\lvert \\phi \\rangle=\\alpha \\lvert 0 \\rangle+\\beta \\lvert 1 \\rangle\n",
    "\\end{equation}\n",
    "\n",
    "\n",
    "The total wave function $ \\lvert \\psi_1 \\rangle$ of all the qubits is\n",
    "\n",
    " \n",
    "\\begin{eqnarray}\n",
    " \\lvert \\psi_1 \\rangle = \\lvert \\phi \\rangle \\otimes \\lvert\\Phi^{+} \\rangle  =\\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_A +\\beta \\lvert 1 \\rangle_A)\\otimes ( \\lvert 0\\rangle_A \\lvert 0 \\rangle_B+ \\lvert 1\\rangle_A \\lvert 1 \\rangle_B)=  \\\\\n",
    "\\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B +\\alpha \\lvert 0 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B + \\beta \\lvert 1 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B + \\beta \\lvert 1 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{eqnarray}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Next, Alice applies the CNOT gate to her two qubits using the unknown qubit as the control and the qubit from the entangled pair as the target\n",
    "\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_2 \\rangle = \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B +\\alpha \\lvert 0 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B + \\beta \\lvert 1 \\rangle_A \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B + \\beta \\lvert 1 \\rangle_A \\lvert 0 \\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Then, Alice applies the Hadamard gate to the qubit in the unknown state\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{3} \\rangle=\\frac{1}{\\sqrt{2^3}}(\\alpha  \\lvert 0 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B + \\alpha  \\lvert 1 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B + \\alpha  \\lvert 0 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B + \\alpha  \\lvert 1 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B+ \\beta  \\lvert 0 \\rangle_A \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B-\\beta  \\lvert 1 \\rangle_A \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B + \\beta  \\lvert 0 \\rangle_A \\lvert 0 \\rangle_A \\lvert 1 \\rangle_B-\\beta  \\lvert 1 \\rangle_A \\lvert 0 \\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Alice measures the state of her two qubits and communicate two bits of classical information to Bob to inform him of the outcome of her measurements. The system collapses to one of the four following states:\n",
    "\n",
    "<ol>\n",
    "\n",
    "<li>\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{4} \\rangle=\\frac{1}{\\sqrt{2}}(\\alpha  \\lvert 0 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B  + \\beta  \\lvert 0 \\rangle_A \\lvert 0 \\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{4} \\rangle=\\frac{1}{\\sqrt{2}}(\\alpha  \\lvert 0 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B  + \\beta  \\lvert 0 \\rangle_A \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{4} \\rangle=\\frac{1}{\\sqrt{2}}(\\alpha  \\lvert 1 \\rangle_A \\lvert 0 \\rangle_A \\lvert 0 \\rangle_B  - \\beta  \\lvert 1 \\rangle_A \\lvert 0 \\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{4} \\rangle=\\frac{1}{\\sqrt{2}}(\\alpha  \\lvert 1 \\rangle_A \\lvert 1 \\rangle_A \\lvert 1 \\rangle_B  - \\beta  \\lvert 1 \\rangle_A \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "</ol>\n",
    "\n",
    "\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Depending on the values of the classical bits which Bob receives from Alice, he will apply one of four possible gates to his qubit\n",
    "\n",
    "<ol>\n",
    "\n",
    "<li>\n",
    "00: Bob applies the identity gate to his qubit and obtains (we neglect the measured qubits)\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{5} \\rangle = I \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_B  + \\beta \\lvert 1   \\rangle_B) = \\lvert \\phi \\rangle\n",
    "\\end{equation}\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "01: Bob applies the $X$ gate to his qubit\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{5} \\rangle = X \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 1 \\rangle_B  + \\beta \\lvert 0   \\rangle_B) = \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_B  + \\beta \\lvert 1   \\rangle_B) = \\lvert \\phi \\rangle\n",
    "\\end{equation}\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "10: Bob applies the $Z$ gate to his qubit\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{5} \\rangle = Z \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_B  - \\beta \\lvert 1   \\rangle_B) = \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_B  + \\beta \\lvert 1   \\rangle_B) = \\lvert \\phi \\rangle\n",
    "\\end{equation}\n",
    "\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "11: Bob applies the $X$ and the $Z$ gate to his qubit\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_{5} \\rangle = ZX \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 1 \\rangle_B  - \\beta \\lvert 0   \\rangle_B) = \\frac{1}{\\sqrt{2}}(\\alpha \\lvert 0 \\rangle_B  + \\beta \\lvert 1   \\rangle_B) = \\lvert \\phi \\rangle\n",
    "\\end{equation}\n",
    "\n",
    "</li>\n",
    "\n",
    "</ol>\n",
    "\n",
    "</li>\n",
    "\n",
    "</ol>\n",
    "\n",
    "Therefore, Bob is able to reproduce the state $\\lvert \\phi \\rangle$ on his qubit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\"> QISKit: implement the quantum teleportation algorithm </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 1) Teleport a qubit in the state $\\frac{1}{\\sqrt{2}}( \\lvert 0 \\rangle+ \\lvert 1 \\rangle)$ </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OPENQASM 2.0;\n",
      "include \"qelib1.inc\";\n",
      "qreg q[3];\n",
      "creg c0[1];\n",
      "creg c1[1];\n",
      "creg c2[1];\n",
      "h q[1];\n",
      "cx q[1],q[2];\n",
      "h q[0];\n",
      "cx q[0],q[1];\n",
      "h q[0];\n",
      "measure q[0] -> c0[0];\n",
      "measure q[1] -> c1[0];\n",
      "if(c0==1) z q[2];\n",
      "if(c1==1) x q[2];\n",
      "measure q[2] -> c2[0];\n",
      "\n",
      "\n",
      "The measured outcomes of the circuits are: {'0 0 0': 119, '0 0 1': 123, '0 1 0': 134, '0 1 1': 121, '1 0 0': 136, '1 0 1': 117, '1 1 0': 145, '1 1 1': 129}\n"
     ]
    }
   ],
   "source": [
    "from qiskit import *\n",
    "\n",
    "# Quantum program setup \n",
    "Q_program = QuantumProgram()\n",
    "\n",
    "# Creating registers\n",
    "q = Q_program.create_quantum_register('q', 3)\n",
    "c0 = Q_program.create_classical_register('c0', 1)\n",
    "c1 = Q_program.create_classical_register('c1', 1)\n",
    "c2 = Q_program.create_classical_register('c2', 1)\n",
    "\n",
    "# Creates the quantum circuit\n",
    "teleport = Q_program.create_circuit('teleport', [q], [c0,c1,c2])\n",
    "\n",
    "# Make the shared entangled state \n",
    "teleport.h(q[1])\n",
    "teleport.cx(q[1], q[2])\n",
    "\n",
    "# Prepare Alice's qubit\n",
    "teleport.h(q[0])\n",
    "\n",
    "# Alice applies teleportation gates (or projects to Bell basis)\n",
    "teleport.cx(q[0], q[1])\n",
    "teleport.h(q[0])\n",
    "\n",
    "# Alice measures her qubits\n",
    "teleport.measure(q[0], c0[0])\n",
    "teleport.measure(q[1], c1[0])\n",
    "\n",
    "# Bob applies certain gates based on the outcome of Alice's measurements\n",
    "teleport.z(q[2]).c_if(c0, 1)\n",
    "teleport.x(q[2]).c_if(c1, 1)\n",
    "\n",
    "# Bob checks the state of the teleported qubit\n",
    "teleport.measure(q[2], c2[0])\n",
    "\n",
    "# Shows gates of the circuit\n",
    "circuits = ['teleport']\n",
    "print(Q_program.get_qasms(circuits)[0])\n",
    "\n",
    "# Parameters for execution on simulator\n",
    "backend = 'local_qasm_simulator' \n",
    "shots = 1024 # the number of shots in the experiment \n",
    "\n",
    "# Run the algorithm\n",
    "result = Q_program.execute(circuits, backend=backend, shots=shots)\n",
    "\n",
    "#Shows the results obtained from the quantum algorithm \n",
    "counts = result.get_counts('teleport')\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:', counts)\n",
    "\n",
    "# credits to:  https://github.com/QISKit/qiskit-tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.2 Superdense coding\n",
    "\n",
    "Here we take a look at the algorithm for superdense coding [2]. This quantum algorithm allows to transfer two bits of classical information just by transferring a qubit. The only requirement is that it is possible to generate an entangled pair of qubit in the Bell state $\\lvert\\Phi^{+} \\rangle$.\n",
    "\n",
    "<img src=\"figures/6/superdense3.jpeg\"  width=\"500\">\n",
    "$$\\text{2. Quantum circuit for superdense coding.}$$\n",
    "\n",
    "The algorithm involves the following steps:\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "As in the quantum teleportation algorithm, Alice and Bob share an entangled pair of qubit in the Bell state to start with.\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_1 \\rangle = \\lvert\\Phi^{+} \\rangle =\\frac{1}{\\sqrt{2}}( \\lvert 0\\rangle_A \\lvert 0 \\rangle_B+ \\lvert 1\\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "\n",
    "<li>\n",
    "Depending on the two classical bits $b_0$, $b_1$ that Alice wants to send to Bob, Alice will apply on of four possible gates $X^{b_0}Z^{b_1}$ to her qubit\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "$b_0b_1=00$: Alice applies $X^{0}Z^{0}=I$ to her qubit. The state of the system is now:\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_2 \\rangle =\\frac{1}{\\sqrt{2}}( \\lvert 0\\rangle_A \\lvert 0 \\rangle_B+ \\lvert 1\\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$b_0b_1=01$: Alice applies $X^{0}Z^{1}=Z$ to her qubit. The state of the system is now:\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_2 \\rangle =\\frac{1}{\\sqrt{2}}( \\lvert 0\\rangle_A \\lvert 0 \\rangle_B - \\lvert 1\\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$b_0b_1=10$: Alice applies $X^{1}Z^{0}=X$ to her qubit. The state of the system is now:\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_2 \\rangle =\\frac{1}{\\sqrt{2}}( \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B+ \\lvert 0\\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$b_0b_1=11$: Alice applies $X^{1}Z^{1}=XZ$ to her qubit. The state of the system is now:\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_2 \\rangle =\\frac{1}{\\sqrt{2}}( \\lvert 1 \\rangle_A \\lvert 0 \\rangle_B - \\lvert 0\\rangle_A \\lvert 1 \\rangle_B)\n",
    "\\end{equation}\n",
    "</li>\n",
    "</ol>\n",
    "\n",
    "</li>\n",
    "\n",
    "\n",
    "<li>\n",
    "Now, Alice sends her qubit to Bob, who can read the two-bit message sent by Alice through her qubit by making a joint measurement of the two qubits in the Bell basis. Since the quantum computer available today can carry out measurement only in the standard (Z) basis, let us see how to convert a two-qubit measurement in the Bell basis into a two-qubit measurement in the standard basis.\n",
    "\n",
    "<img src=\"figures/6/bell_st1.jpeg\"  width=\"300\">\n",
    "$$\\text{3. Equivalence between different measurement bases.}$$\n",
    "\n",
    "\n",
    "\n",
    "Measuring two qubits in the Bell basis is equivalent to performing a CNOT gate $CX_{12}$, where the first qubit is the control qubit and the second qubit is the target one, and an Hadamard gate $H$ on the first qubit and then measuring in the standard basis. The quantum circuit with measurements in the standard basis is depicted in Fig. 4.\n",
    "\n",
    "<img src=\"figures/6/superdense_st1.jpeg\"  width=\"550\">\n",
    "$$\\text{4. Superdense coding quantum circuit with measurements in the standard basis.}$$\n",
    "\n",
    "Bob's possible outcomes are:  \n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_3 \\rangle =\\frac{1}{2}( \\lvert 0\\rangle_B \\lvert 0 \\rangle_B + \\lvert 1\\rangle_B \\lvert 0 \\rangle_B + \\lvert 0\\rangle_B \\lvert 0 \\rangle_B - \\lvert 1\\rangle_B \\lvert 0 \\rangle_B) = \\lvert 0\\rangle_B \\lvert 0 \\rangle_B\n",
    "\\end{equation}\n",
    "\n",
    "Therefore, Bob decodes $b_0b_1=00$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_3 \\rangle =\\frac{1}{2}( \\lvert 0\\rangle_B \\lvert 0 \\rangle_B + \\lvert 1\\rangle_B \\lvert 0 \\rangle_B - \\lvert 0\\rangle_B \\lvert 0 \\rangle_B + \\lvert 1\\rangle_B \\lvert 0 \\rangle_B) = \\lvert 1\\rangle_B \\lvert 0 \\rangle_B\n",
    "\\end{equation}\n",
    "\n",
    "Therefore, Bob decodes $b_0b_1=01$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_3 \\rangle =\\frac{1}{2}( \\lvert 0\\rangle_B \\lvert 1 \\rangle_B - \\lvert 1\\rangle_B \\lvert 1 \\rangle_B + \\lvert 0\\rangle_B \\lvert 1 \\rangle_B + \\lvert 1\\rangle_B \\lvert 1 \\rangle_B) = \\lvert 0\\rangle_B \\lvert 1 \\rangle_B\n",
    "\\end{equation}\n",
    "\n",
    "Therefore, Bob decodes $b_0b_1=10$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "\\begin{equation}\n",
    "\\lvert \\psi_3 \\rangle =\\frac{1}{2}( \\lvert 0\\rangle_B \\lvert 1 \\rangle_B - \\lvert 1\\rangle_B \\lvert 1 \\rangle_B - \\lvert 0\\rangle_B \\lvert 1 \\rangle_B - \\lvert 1\\rangle_B \\lvert 1 \\rangle_B) = - \\lvert 1 \\rangle_B \\lvert 1 \\rangle_B\n",
    "\\end{equation}\n",
    "\n",
    "Therefore, Bob decodes $b_0b_1=11$\n",
    "</li>\n",
    "</ol>\n",
    "\n",
    "</li>\n",
    "\n",
    "</ol>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\"> QISKit: implement the superdense coding algorithm </span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <span style=\"color:blue\"> 3) Send the two bits $01$ through superdense coding </span>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List of gates:\n",
      "h\n",
      "cx\n",
      "x\n",
      "cx\n",
      "h\n",
      "measure\n",
      "measure\n",
      "\n",
      "The measured outcomes of the circuits are: {'10': 1024}\n"
     ]
    }
   ],
   "source": [
    "from initialize import *\n",
    "from qiskit import *\n",
    "\n",
    "\n",
    "#initialize quantum program\n",
    "my_alg = initialize(circuit_name = 'superdense', qubit_number=2, bit_number=2, backend = 'local_qasm_simulator', shots = 1024)\n",
    "\n",
    "#add gates to the circuit\n",
    "\n",
    "#creates a bell pair\n",
    "my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit\n",
    "my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) ## applies CX gate\n",
    "\n",
    "#Alice encodes 01\n",
    "my_alg.q_circuit.x(my_alg.q_reg[0])\n",
    "\n",
    "#to measure in the Bell basis, Bob does the following operations before measuring in the standard basis\n",
    "my_alg.q_circuit.cx(my_alg.q_reg[0],my_alg.q_reg[1]) ## applies CX gate\n",
    "my_alg.q_circuit.h(my_alg.q_reg[0]) # applies H gate to first qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[0], my_alg.c_reg[0]) # measures the first qubit\n",
    "my_alg.q_circuit.measure(my_alg.q_reg[1], my_alg.c_reg[1]) # measures the second qubit\n",
    "\n",
    "\n",
    "print('List of gates:')\n",
    "for circuit in my_alg.q_circuit:\n",
    "    print(circuit.name)\n",
    "\n",
    "#Execute the quantum algorithm\n",
    "result = my_alg.Q_program.execute(my_alg.circ_name, backend=my_alg.backend, shots= my_alg.shots)\n",
    "\n",
    "#Show the results obtained from the quantum algorithm \n",
    "counts = result.get_counts(my_alg.circ_name)\n",
    "\n",
    "print('\\nThe measured outcomes of the circuits are:',counts)\n",
    "\n",
    "# credits to:  https://github.com/QISKit/qiskit-tutorial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises\n",
    "\n",
    "<ol>\n",
    "<li>\n",
    "Show the step-by-step evolution of the total state of the system $\\lvert \\psi \\rangle$ for the quantum teleportation algorithm for the following states to be teleported:\n",
    "\n",
    "<ol>\n",
    "\n",
    "<li>\n",
    "$\\lvert 1 \\rangle$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$\\frac{1}{\\sqrt{1}} ( \\lvert 0 \\rangle+ \\lvert 1 \\rangle )$\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "$\\sqrt{\\frac{2}{3}} \\lvert 0 \\rangle+\\frac{1}{\\sqrt{3}} \\lvert 1 \\rangle$\n",
    "</li>\n",
    "\n",
    "</ol>\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Design quantum circuits for the quantum teleportation of the states given in problem 1.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Write QISKit programs which implement the quantum teleportation algorithm for each state specified in problem 1.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Alice wants to send 4 classical bits $b_0b_1b_2b_3=0110$ to Bob by sending only two qubits. Explain how this can be done. Then, design a quantum circuit which implements it.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Show the step-by-step evolution of the total state of the system for the situation described in problem 4.\n",
    "</li>\n",
    "\n",
    "<li>\n",
    "Write a QISKit program which implements the superdense coding algorithm specified in problem 4.\n",
    "</li>\n",
    "\n",
    "\n",
    "\n",
    "</ol>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "[1] C. H. Bennett et al., Physical Review Letters, 70, 1895 (1993).\n",
    "\n",
    "[1] C. H. Bennett and S. J. Wiesner, Physical Review Letters, 69, 2881\n",
    "(1992).\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "[3] K. Mattle et al., Physical Review Letters, 76, 4656 (1996).\n",
    "\n",
    "[4] M. Riebe et al., Nature, 429,734 (2004).\n",
    "\n",
    "[5] M. D. Barrett et al., Nature, 429, 737 (2004).\n",
    "\n",
    "[6] H. J. Kimble and S. J. van Euk, Nature, 429, 712 (2004).\n",
    "\n",
    "[7] M. B. Plenio and V. Vedral, Contemporary Physics, 39, 431 (1998).\n",
    "\n",
    "[8] D. Bouwmeester et al., Nature, 390, 575 (1997)\n",
    "\n",
    "[9] S. Fedortchenko, arXiv:1607.02398 (2016).\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
